package com.letv.mytest.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jmind.core.spring.MappingFastjsonHttpMessageConverter;
import jmind.core.support.HttpUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.letv.mytest.model.User;

@RequestMapping("/dev")
@Controller
public class DEVController {

    @RequestMapping(value = "/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
    public String regularExpression(@PathVariable String textualPart, @PathVariable String numericPart) {
        System.out.println("Textual part: " + textualPart + ", numeric part: " + numericPart);
        return "someResult";
    }

    @RequestMapping(value = "/")
    @ResponseBody
    public Date t() {

        return new Date();
    }

    @RequestMapping(value = "/u")
    @ResponseBody
    public String uu(User user) {
        System.out.println(user);
        user.setAge(18);
        System.out.println(user);
        return user.toString();
    }

    @RequestMapping(value = "/u2", method = RequestMethod.GET)
    @ResponseBody
    public User u2(User user) {
        user.setLoginTime(new Date());
        System.out.println(user);
        user.setAge(18);
        System.out.println(user);
        return user;
    }

    @RequestMapping(value = "/u1", method = RequestMethod.GET)
    @ResponseBody
    public User u1(@ModelAttribute User user) {
        System.out.println(user);
        user.setAge(18);
        System.out.println(user);
        return user;
    }

    @RequestMapping("/get")
    public String get(final HttpServletRequest request, final HttpServletResponse response, @RequestParam int id,
            @RequestParam(required = false) String name, @RequestHeader("Accept") String ContentType) {
        System.out.println(ContentType);
        User pojo = new User();
        pojo.setUid(id);
        pojo.setName(name);

        request.setAttribute("pojo", pojo);
        System.out.println(response.getContentType());

        return "pojo";
    }

    @RequestMapping("/get1")
    @ResponseBody
    public User get1(final HttpServletRequest request, final HttpServletResponse response, @RequestParam int id,
            @RequestParam(required = false) String name, @RequestHeader("User-Agent") String ua) {
        System.out.println(ua);
        User pojo = new User();
        pojo.setUid(id);
        pojo.setName(name);

        request.setAttribute("pojo", pojo);
        System.out.println(response.getContentType());

        return pojo;
    }

    /**
     * @RequestBody 需要配置解析，参见servlet—context.xml 配置
     * 可以用 @ModelAttribute 替换,不需要配置
     * 2014-1-3 
     * @param pojo
     * @param response
     * @see MappingFastjsonHttpMessageConverter
     * @return
     */
    @RequestMapping("/get2")
    @ResponseBody
    public User get2(@RequestBody User pojo, final HttpServletResponse response) {
        System.out.println(pojo);

        System.out.println(response.getContentType());
        return pojo;
    }

    @RequestMapping("/get3")
    @ResponseBody
    public String get3(User pojo) {
        System.out.println(pojo);

        return pojo.toString();
    }

    public static void main(String[] args) {
        Map<String, String> params = Maps.newHashMap();
        params.put("name", "转发给");
        params.put("age", "12");

        String post = HttpUtil.post("http://test.letv.com/dev/get2", params);
        System.out.println(post);

    }

    /**
     * /dev/get4?id=123&name=dsfdsf
     * 2014-2-20 
     * @param pojo
     * @param ua
     * @return
     */
    @RequestMapping("/get4")
    @ResponseBody
    public User get4(@ModelAttribute User pojo, @RequestHeader("User-Agent") String ua) {
        pojo.setCode("code");
        System.out.println(pojo);
        pojo.setName("ee");
        System.out.println(ua);
        return pojo;
    }

    /**
    SpringMvc 可以接收将前端传递的数组，可以使用转换为List 或者数组接收。 没用ajax 测试

    前端做法:
    1   $.ajax({
    2         type:'post',
    3         url :'test',
    4         data:{
    5             name : 'nick',
    6             kind : [20,30,40]
    7         }
    8     })

    后端接收:
    1   @RequestMapping("test")
    2   public void test(String name, @RequestParam(value = "kind[]", defaultValue = "") List<Integer> kind, HttpServletRequest request, Writer writer) {
    3    
    4   }
     */
    // ids=1&ids=22&ids=101
    @RequestMapping("/get5")
    @ResponseBody
    public List<Integer> get5(@RequestParam List<Integer> ids) {
        System.out.println(ids);

        return ids;

    }

}
